<!--
 * Copyright 2022 The kubegems.io Authors
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
-->

<template>
  <div class="pt-4">
    <component :is="formComponent" :ref="formComponent" :v="v" @close="close" />
  </div>
</template>

<script>
  import MetricsBaseForm from './MetricsBaseForm';
  import MetricsDocs from './MetricsDocs';

  export default {
    name: 'Metrics',
    components: {
      MetricsBaseForm,
      MetricsDocs,
    },
    props: {
      v: {
        type: String,
        default: () => '',
      },
      component: {
        type: String,
        default: () => 'MetricsDocs',
      },
    },
    data() {
      return {
        formComponent: 'MetricsDocs',
      };
    },
    watch: {
      component: {
        handler(newValue) {
          if (newValue) {
            this.formComponent = newValue;
          }
        },
        deep: true,
        immediate: true,
      },
    },
    methods: {
      switchComponent() {
        this.formComponent = this.formComponent === 'MetricsBaseForm' ? 'MetricsDocs' : 'MetricsBaseForm';
      },
      async addData() {
        this.$refs[this.formComponent].addData();
      },
      close() {
        this.$emit('close');
      },
    },
  };
</script>

<style lang="scss" scoped>
  .metrics {
    &__fab {
      margin-bottom: 55px;
      height: 45px;
      width: 45px;
      border-radius: 45px;
      right: 20px;
    }
  }
</style>
